import pickle

from distributed.protocol.serialize import serializers
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
from django.views.decorators.csrf import csrf_exempt

from cmdb.CoinStockEntity import CoinStockEntity
from cmdb.Department import Department
from cmdb.HttpClientUtils import HttpClientUtils
from django.core import serializers
import json


def index(request):
    # return HttpResponse("hello world")

    return render(request, "index.html")


def test(request):
    # return HttpResponse("hello world")

    return render(request, "test.html")

def test2(request):
    # return HttpResponse("hello world")

    return render(request, "test2.html")



@csrf_exempt #This skips csrf validation. Use csrf_protect to have validation
def GetDepartment(request):
    response_data = {}
    lstRes = []

    json_uyulin = httpclienttouyulin()
    json_suiqiu = httpclienttosuiqiu()['data'][0]

    u_yulin = CoinStockEntity(webTitle="优雨林",currentPrice=json_uyulin['menu']['wkc_doge']['price'], totalVol=json_uyulin['top'][5]
                              , riseRange=json_uyulin['top'][6])

    lstRes.append(u_yulin)
    response_data["rows"]=lstRes
    return HttpResponse(json.dumps(response_data, default=lambda obj: obj.__dict__), content_type="application/json")



def httpclienttouyulin():
    body = HttpClientUtils.synchronous_fetch("https://www.uyulin.com/trade/index_json/market/wkc_doge")
    body = bytes.decode(body)

    jsonObject = json.loads(body)
    return jsonObject


def httpclienttosuiqiu():
    body = HttpClientUtils.synchronous_fetch("https://www.chaobi.la/api/market/market_info")
    body = bytes.decode(body)

    jsonObject = json.loads(body)
    return jsonObject